package com.jackrain.nea.web.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.core.schema.Table;
import com.jackrain.nea.core.schema.TableManager;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.web.QueryListCmd;
import com.jackrain.nea.web.face.impl.UserImpl;
import com.jackrain.nea.web.query.QuerySession;
import com.jackrain.nea.web.query.QueryUtils;
import com.jackrain.nea.web.security.Security4Utils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@Api(value = "TableQueryController", description = "列表查询数据rest")
public class QueryListController {

    @Reference(version = "1",group = "ad")
    private QueryListCmd queryListCmd1;

    @Reference(version = "2",group = "cp")
    private QueryListCmd queryListCmd2;

    @Reference(version = "3",group = "ps")
    private QueryListCmd queryListCmd3;

    @Reference(version = "4",group = "vp")
    private QueryListCmd queryListCmd4;

    @Autowired
    private PropertiesConf pconf;

    private String service = "com.jackrain.nea.web.QueryListCmd";



//    @Autowired
//    private AjaxUtils ajaxUtils;


    @RequestMapping(path = "/p/cs/QueryList", method = {RequestMethod.GET, RequestMethod.POST})
    public JSONObject getTableQuery(HttpServletRequest request,
                                    @RequestParam(value = "searchdata", required = true) String obj) throws Exception {
        log.debug("query data ->("+ obj+")");

        JSONObject jo=JSON.parseObject(obj);
        UserImpl userWeb = (UserImpl) request.getSession().getAttribute("user");
        QuerySession qsession = QueryUtils.createQuerySession(userWeb);
        TableManager tm = qsession.getTableManager();

        Table table=null;

        int tableid=jo.getIntValue("tableid") ;
        int refcolid=jo.getIntValue("refcolid") ;

        if(refcolid>0) {
            table = tm.getColumn(refcolid).getReferenceTable();
        }

        if(tableid>0&&table==null) {
            table = tm.getTable(tableid);
        }

        if(jo.getString("table")!=null&&table==null) {
            table = tm.getTable(jo.getString("table"));
        }

        if(table==null) {
            throw  new NDSException("table is null");
        }

        String center  = table.getCategory().getSubSystem().getCenter();
        String[] gv = center.split(":");
        if(gv.length != 2){
            throw new NDSException("center is error");
        }
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), service,gv[0],gv[1]);
        log.info("edas service->"+service+":"+gv[0]+":"+gv[1]);

        JSONObject result = ((QueryListCmd) o).getTableQuery(userWeb,obj);
        return result;

    }

    @RequestMapping(path = "/fc/api/QueryList", method = {RequestMethod.GET, RequestMethod.POST})
    public String getTableQueryApi(HttpServletRequest request,
                                    @RequestParam(value = "searchdata", required = true) String obj) throws Exception {
        log.debug("query data ->("+ obj+")");

        JSONObject jo=JSON.parseObject(obj);
        UserImpl userWeb = (UserImpl) Security4Utils.getUser(request.getParameter("username"));
        QuerySession qsession = QueryUtils.createQuerySession(userWeb);
        TableManager tm = qsession.getTableManager();

        Table table=null;

        int tableid=jo.getIntValue("tableid") ;
        int refcolid=jo.getIntValue("refcolid") ;

        if(refcolid>0) {
            table = tm.getColumn(refcolid).getReferenceTable();
        }

        if(tableid>0&&table==null) {
            table = tm.getTable(tableid);
        }

        if(jo.getString("table")!=null&&table==null) {
            table = tm.getTable(jo.getString("table"));
        }

        if(table==null) {
            throw  new NDSException("table is null");
        }

        String center  = table.getCategory().getSubSystem().getCenter();
        String[] gv = center.split(":");
        if(gv.length != 2){
            throw new NDSException("center is error");
        }
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), service,gv[0],gv[1]);
        log.info("edas service->"+service+":"+gv[0]+":"+gv[1]);

        JSONObject result = ((QueryListCmd) o).getTableQuery(userWeb,obj);
        if(result != null){
            return result.toJSONString();
        }else{
            return "";
        }

    }

}
